﻿using PracticeCentre.Common;
using PracticeCentre.DataControl;
using PracticeCentre.Modeles;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PracticeCentre.Business
{
    public class PersonalInfo
    {
        /// <summary>
        /// 分页获取人员信息列表
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public PageModel<Personal> GetPersonalList(PageModel<Personal> page)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@sys_PageIndex",page.PageNumber),
                    new SqlParameter("@sys_PageSize",page.PageSize),
                    new SqlParameter("@sys_Table","PersonalInfo"),
                    new SqlParameter("@sys_Key","id"),
                    new SqlParameter("@sys_Where",""),
                    new SqlParameter("@sys_Order","id"),
                    new SqlParameter("@sys_AscOrDesc",""),
                    new SqlParameter("@pageCount",page.PageCount),
                    new SqlParameter("@recordCount",page.RowCount)
                };
                parameters[7].Direction = ParameterDirection.Output;
                parameters[8].Direction = ParameterDirection.Output;
                int pagecount = 0;
                int rowcount = 0;
                DataTable dt = dataHelper.ExecuteDataTable<Personal>(ProceduresConfig.GeneralPaging, out pagecount, out rowcount, parameters);
                DataModelHandler<Personal> dataHandler = new DataModelHandler<Personal>();
                List<Personal> postList = dataHandler.FillModel(dt);
                page.PageCount = pagecount;
                page.RowCount = rowcount;
                page.DataList = postList;
                return page;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据员工编号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Personal GetModelByJobNumber(Employee model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@JobNumber",model.JobNumber)
                };
                DataModelHandler<Personal> modeHandler = new DataModelHandler<Personal>();
                List<Personal> personalList = modeHandler.FillModel(dataHelper.ExecuteDataTable(ProceduresConfig.PersonalInfoGetModelByJobNumber, parameters));
                return personalList.Count > 0 ? personalList[0] : null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据ID获取个人信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Personal GetModelById(Personal model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@Id",model.Id)
                };
                DataModelHandler<Personal> modeHandler = new DataModelHandler<Personal>();
                List<Personal> personalList = modeHandler.FillModel(dataHelper.ExecuteDataTable(ProceduresConfig.PersonalInfoGetModelById, parameters));
                return personalList.Count > 0 ? personalList[0] : null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 添加个人信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(Personal model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@Name",model.Name),
                    new SqlParameter("@Sex",model.Sex),
                    new SqlParameter("@Age",model.Age),
                    new SqlParameter("@Birthday",model.Birthday),
                    new SqlParameter("@IdNumber",model.IdNumber),
                    new SqlParameter("@PhoneNumber",model.PhoneNumber),
                    new SqlParameter("@Address",model.Address)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.AddPersonalInfo, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 更新个人信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(Personal model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@Id",model.Id),
                    new SqlParameter("@Name",model.Name),
                    new SqlParameter("@Sex",model.Sex),
                    new SqlParameter("@Age",model.Age),
                    new SqlParameter("@Birthday",model.Birthday),
                    new SqlParameter("@IdNumber",model.IdNumber),
                    new SqlParameter("@PhoneNumber",model.PhoneNumber),
                    new SqlParameter("@Address",model.Address)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.UpdateEmployees, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        


    }
}
